<?php
require_once '_mysql.php';
class QR {
	public function __construct() {}
	public function get_total($dbtable, $str){
		global $db;
		$sql="select count(*) as total from $dbtable where $str";
		//echo $sql;
		$query=$db->query($sql);
		$result=$db->fetch_array($query);
		return $result['total'];
	}
	public function get_data($dbtable,$field,$str,$from,$count){
		global $db;
		$sql="select $field from $dbtable where $str limit $from,$count";
		//echo $sql;exit;
		$query=$db->query($sql);
		$i=0;
		$data=array();
		while($rs=$db->fetch_array($query)) {
			$data[$i]=$rs;
			++$i;
		}
		return $data;
	}
	public function get_line($dbtable,$field,$str,$from,$count){
		global $db;
		$sql="select $field from $dbtable where $str limit $from,$count"; 
		//echo $sql;
		$query=$db->query($sql); 
		$i=0;
		$data=array();
		while($rs=$db->fetch_array($query)) {
			$data[$i]->start_en= $rs["start_en"];
			$data[$i]->start_cn= $rs["start_cn"];
			$data[$i]->start_pv= $rs["start_pv"];
			$data[$i]->start_box= $rs["start_box"];
			$data[$i]->start_st= $rs["start_st"];
			$data[$i]->end_st= $rs["end_st"];
			$data[$i]->border = $rs["border"];
			$data[$i]->end_code = $rs["end_code"];
			$data[$i]->end_en = $rs["end_en"];
			$data[$i]->end_cn = $rs["end_cn"];
			$data[$i]->end_ru = $rs["end_ru"];
			$data[$i]->end_cy = $rs["end_cy"];
			$data[$i]->p20 = $rs["p20"];
			$data[$i]->p40gp = $rs["p40gp"];
			$data[$i]->p40hc = $rs["p40hc"];
			$data[$i]->remark = $rs["remark"];
			++$i;
		}
		return $data;
	}
	public function add_data($dbtable, $data){
		global $db;
		$i=0;$tkey="";$tval="";
		foreach ($data as $key=>$val)
		if($key==0){
			$tkey=$val;
		}else{
			$tval=$val;
			$sql = "INSERT INTO $dbtable ($tkey) VALUES ($tval)";
			//echo $sql."</br>";
			$query = $db->query($sql);
		}
		if(!$query)return null;
		return $db->insert_id();
	}
	public function update_data($dbtable,$sql){
		global $db;
		$sql="update $dbtable ".$sql;
		//echo $sql;
		$query=$db->query($sql);
		return $db->affected_rows();
	}
	public function del_data($dbtable,$sql){
		global $db;
		$sql="delete from $dbtable where ".$sql;
		//echo $sql;
		$query=$db->query($sql);
		return $db->affected_rows();
	}
	public function check_exist($dbtable,$str){
		global $db;
		$sql="select * from $dbtable where $str";
		//echo $sql;
		$query=$db->query($sql);
		$rs=$db->fetch_array($query);
		return $rs;
	}	
	public function get_field($dbtable,$field){
		global $db;
		$sql="select distinct $field from $dbtable";
		$query=$db->query($sql);
		$i=0;
		while($rs=$db->fetch_array($query)) {
			$data[$i]= $rs[$field];
			$i++;
		}
		return $data;
	}	
	
}